package com.eachonline.smart.web.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eachonline.core.JsonResult;
import com.eachonline.core.Pagination;
import com.eachonline.smart.model.SmartUser;
import com.eachonline.smart.service.SmartUserService;
import com.eachonline.util.StringUtil;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
	
	private static final Logger logger =  Logger.getLogger(TeacherController.class);
	
	@Autowired
	public SmartUserService userService;
	
	/**
	 * 教师列表
	 * @param session
	 * @param response
	 * @param site
	 * @param teacherName
	 * @return
	 */
    @RequestMapping(value="/queryTeacherList", method = { RequestMethod.POST })
    @ResponseBody
    public JsonResult queryTeacherList(HttpServletRequest request,HttpServletResponse  response,String site,String teacherName){
    	
    	response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		
		int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		int begin =pageSize*(start-1);
		
		Pagination<SmartUser> userPageInfo= userService.queryTeacherListBySite(site, begin, pageSize, teacherName);
		List<SmartUser>  userList=userPageInfo.getDataList();
		if(userList==null||userList.size()==0)
		return  new JsonResult(false, "没用教师数据，请稍后再试");
		
		
		for(SmartUser user :userList)
		{
			user.setPassword(null);
			user.setCreateTime(null);
			user.setLastLoginTime(null);
			user.setLoginName(null);
		}
		
		Map<String,Object>  resultMap=new HashMap<String, Object>();
		
		resultMap.put("teacherList", userList);
		return  new JsonResult(true,resultMap);
		
    }
    

	/**
	 * 教师列表
	 * @param session
	 * @param response
	 * @param site
	 * @param teacherName
	 * @return
	 */
    @RequestMapping(value="/manager/queryTeacherList", method = { RequestMethod.POST })
    @ResponseBody
    public JsonResult querySysTeacherList(HttpServletRequest request,HttpServletResponse  response,String site,String teacherName){
    	
    	response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		
		int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		int begin =pageSize*(start-1);
		
		Pagination<SmartUser> userPageInfo= userService.queryTeacherListBySite(site, begin, pageSize, teacherName);
		List<SmartUser>  userList=userPageInfo.getDataList();
		if(userList==null||userList.size()==0)
		return  new JsonResult(false, "没用教师数据，请稍后再试");
		
		
		for(SmartUser user :userList)
		{
			user.setPassword(null);
			/*user.setCreateTime(null);
			user.setLastLoginTime(null);*/
			user.setLoginName(null);
		}
		
		Map<String,Object>  resultMap=new HashMap<String, Object>();
		
		resultMap.put("teacherList", userList);
		resultMap.put("sumRow", userPageInfo.getTotalRowNumber());
		return  new JsonResult(true,resultMap);
		
    }
    
    
    /**
	 * 教师列表
	 * @param session
	 * @param response
	 * @param site
	 * @param teacherName
	 * @return
	 */
    @RequestMapping(value="/manager/updateTeacherSite", method = { RequestMethod.POST })
    @ResponseBody
    public JsonResult pushTeacherOnHome(HttpServletRequest request,HttpServletResponse  response,String site,String teacherId){
    	
    	response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
	     SmartUser user =userService.selectByPrimaryKey(teacherId);
	     if(user==null)
	     {
	    		return  new JsonResult(false, "没用教师数据，请稍后再试");
	     }else
	     {
	    	 
	    	 user.setUserSite(site);
	    	 userService.updateByPrimaryKeySelective(user);
	    		return  new JsonResult(true,"推荐成功");
	     }
	     
	     
	 
	
	
		
    }
    
    /**
	 * 教师列表
	 * @param session
	 * @param response
	 * @param site
	 * @param teacherName
	 * @return
	 */
    @RequestMapping(value="/queryTeacherDetail", method = { RequestMethod.POST })
    @ResponseBody
    public JsonResult queryTeacherList(HttpServletRequest request,HttpServletResponse  response,String sid){
    	
    	
    	response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
	    SmartUser user=	userService.selectByPrimaryKey(sid);
	    Map<String,Object> teacherInfo=new HashMap<String, Object>();
	    teacherInfo.put("teahcerName", user.getUserName());
	    teacherInfo.put("teacherPicUrl", user.getUserHeadPicUrl());
	    teacherInfo.put("teacherIntro", user.getUserInfo());
		Map<String,Object>  resultMap=new HashMap<String, Object>();
		
		resultMap.put("teacherInfo", teacherInfo);
		return  new JsonResult(true,resultMap);
		
    }
    

}
